﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MbWsClient.ServiceClasses;
using MbWsClient.Service.Enums;
using System.IO;
using System.Runtime.Serialization.Json;
using MbWsClient.Enum;
using System.Net;

namespace MbWsClient.Service
{
    public class MediaBrowserWsClient
    {
        //[OperationContract]
        //[WebGet(UriTemplate = "/item?id={id}", ResponseFormat = WebMessageFormat.Json)]
        public MetadataItem GetItem(Guid id)
        {
            string item = string.Format(@"item?id={0}", id.ToString());
            return CallServiceWithResponse<MetadataItem>(item);
        }

        //[OperationContract]
        //[WebGet(UriTemplate = "/startup", ResponseFormat = WebMessageFormat.Json)]
        public BasicCollectionItem GetStartUpFolders()
        {
            string startup = "startup";
            return CallServiceWithResponse<BasicCollectionItem>(startup);
        }

        //[OperationContract]
        //[WebGet(UriTemplate = "/collection?id={id}", ResponseFormat = WebMessageFormat.Json)]
        public BasicCollectionItem GetCollection(Guid id)
        {
            string item = string.Format("collection?id={0}", id.ToString());
            return CallServiceWithResponse<BasicCollectionItem>(item);
        }

        //[OperationContract]
        //[WebGet(UriTemplate = "/play?id={id}", ResponseFormat = WebMessageFormat.Json)]
        public bool PlayItem(Guid id)
        {
            string play = string.Format("play?id={0}", id.ToString());
            return CallServiceWithResponse<bool>(play);
        }

        /// <summary>
        /// Retrieves and image for a particular guid
        /// </summary>
        /// <param name="id">Id of the MediaItem</param>
        /// <param name="imageType">The type of image</param>
        /// <param name="maxHeight">O or int.MaxValue to ignore</param>
        /// <param name="maxWidth">O or int.MaxValue to ignore</param>
        /// <returns></returns>
        //[OperationContract]
        //[WebGet(UriTemplate = "/image?id={id}&imagetype={imageType}&maxheight={maxHeight}&maxwidth={maxWidth}", ResponseFormat = WebMessageFormat.Json)]
        public PictureItem GetImage(Guid id, ImageTypes imageType, int maxHeight, int maxWidth)
        {
            string image = string.Format("image?id={0}&imagetype={1}&maxheight={2}&maxwidth={3}",
                id.ToString(), imageType, maxHeight, maxWidth);
            return CallServiceWithResponse<PictureItem>(image);
        }

        //[OperationContract]
        //[WebGet(UriTemplate = "/command?type={command}")]
        public void SendCommand(CommandTypes command)
        {
        }

        private Stream CallService(string parms)
        {
            HttpWebRequest request = CreateJSONWebRequest(parms);
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException we)
            {
                response = (HttpWebResponse)we.Response;
            }

            return response.GetResponseStream();
        }

        private T CallServiceWithResponse<T>(string parms)
        {
            Stream stream = CallService(parms);
            return Deserialise<T>(stream);
        }

        private T Deserialise<T>(Stream stream)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            return (T)ser.ReadObject(stream);
        }

        public static HttpWebRequest CreateWebRequest(string parms)
		{
            string uri = ConfigHelper.GetConfigValue(ConfigSetting.ServiceAddress);
            uri += parms;
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.Method = "GET";
			return httpWebRequest;
		}

		public static HttpWebRequest CreateJSONWebRequest(string parms)
		{
			HttpWebRequest request = CreateWebRequest(parms);
			request.Accept = "application/json";
			return request;
		}
    }
}
